<template>
  <div class="updateCar">
  	  <x-header :title="headerTitle" :left-options="{backText:''}" class='whiteHeader'></x-header>
      <div class="updateCarH" @click='selectCar'>
        <img :src="logo" alt="" />
        <div class="carMess">
          <h1>{{title.split(' ')[0]}}</h1>
          <p>{{title.split(' ')[1]}} {{title.split(' ')[2]}}</p>
        </div>
        <img src="../../assets/image/address/carRight.png" alt="" class="right" />
      </div>
      <div class="carCard item border-bottom">
        <label>车牌号码</label>
        <input type="text" placeholder="请输入完整的车牌号码" v-model='carPhone' @click='brandFocus' onfocus="this.blur();" :class='{inputActive:showFlags==1}'/>
      </div>
      <div class="lichen item border-bottom" v-if='from!="illegal"'>
        <label><span>里</span><span>程</span></label>
        <input type="text" placeholder="请输入里程" v-model='mileage' @click='mileFocus'/>
      </div>
      <div class="code item border-bottom">
        <label>车架号码</label>
        <input type="text" placeholder="请输入车架号码后六位" v-model='carCode' @click='codeFocus' onfocus="this.blur();" :class='{inputActive:showFlags==2}'/>
        <img src="../../assets/image/maintain/question.png" alt="" @click='showBrand'/>
      </div>
      <div class="Engine item border-bottom">
        <label>发动机号</label>
        <input type="text" placeholder="请输入发动机后六位" v-model='engineCode' @click='engineFocus' onfocus="this.blur();" :class='{inputActive:showFlags==3}'/>
        <img src="../../assets/image/maintain/question.png" alt="" @click='showBrand'/>
      </div>
      <div class="save" @click='addCar' v-if='from!="illegal"'>
         保存
      </div>
      <div class="save illegalBtn" @click='illegalCheck' v-if='from=="illegal"'>
          开始查询
      </div>
      <div class="illegalR" v-if='from=="illegal"'>
          <div class="illegalTitle">
            <img src="../../assets/image/illegal/title.png" alt="">
          </div>
          <div class="illegalFlag">
            <div class="flag">
               <img src="../../assets/image/illegal/flag1.png" alt="">
               <p>查询准确</p>
            </div>
            <div class="flag">
               <img src="../../assets/image/illegal/flag2.png" alt="">
               <p>快速查询</p>
            </div>
            <div class="flag">
               <img src="../../assets/image/illegal/flag3.png" alt="">
               <p>免费查询</p>
            </div>
            <div class="flag">
              <img src="../../assets/image/illegal/flag4.png" alt="">
              <p>信息保密</p>
            </div> 
          </div>
      </div>
      <div :class="{keyBoards:keyBoards}">
      <vueCustomKeyBoard :openLevel1='showBrand1' :openLevel2='showNum1' @brand='brand' @change='change' @broadcast='broadcast' :flag='true' :show4='show4'></vueCustomKeyBoard>
      <vueCustomKeyBoard :openLevel1='showBrand2' :openLevel2='showNum2' @broadcast='broadcast' @brand='brand' @change='change' :maxLength='6' :show4='show4'></vueCustomKeyBoard>
      <vueCustomKeyBoard :openLevel1='showBrand3' :openLevel2='showNum3' @broadcast='broadcast' @brand='brand' @change='change' :maxLength='6' :show4='show4'></vueCustomKeyBoard>
      </div>
      <x-dialog v-model="showToast">
      
        <img src="../../assets/image/carB.png" alt="" class="showImg" @click='closeBrand'/>
      </x-dialog>
  </div>
</template>

<script>
// 更新车辆信息模块
import {XHeader,XDialog} from 'vux';
import vueCustomKeyBoard from '../../components/vue-custom-keyBoard';
import { setCookie, getCookie } from '../../util/commonUtils';
export default {
  name:"updateCar",
  // 数据
  data(){
    return{
      userId:'',//用户id
      headerTitle:'',//标题
    	title:'',//车辆名字-车型-车系
      logo:'',//车辆的logo
      carPhone:'',//车牌号
      mileage:'',//里程数
      carCode:'',//车架号
      engineCode:'',//发动机号
      showBrand1:false,//显示省份简称键盘
      showNum1:false,//显示26个字母和数字键盘
      deleteFlag:false,  //删除操作标识
      showBrand2:false,//车架号
      showNum2:false,
      showBrand3:false,//发动机号
      showNum3:false,
      show4:false,
      editMess:'',   //编辑车辆的信息
      addCarMess:'',   //添加车辆信息
      showToast:false, //
      from:'',  //来自哪里
      keyBoards:false,
      keyFlag:false,
      showFlags:0  //控制是否点击哪一个输入框高亮
    }
  },
  components: {
    XHeader,
    vueCustomKeyBoard,
    XDialog
  },
  // 计算属性
  computed:{
  },
  // 创建（实例创建完成）
  created(){
    this.userId=getCookie('loginkey');
    this.from=localStorage.getItem('from');
    if(localStorage.getItem('addCar')){//来自添加
      this.headerTitle='添加车辆';
      let obj=JSON.parse(localStorage.getItem('addCar'));
      this.addCarMess=obj;
      this.title=obj.carName;
      this.logo=obj.carPublic+obj.SICON;
    }else if(localStorage.getItem('editCar')){//来自编辑
      let publicUrl=localStorage.getItem('publicUrl'); 
      this.headerTitle='编辑车辆';
      let editMess=JSON.parse(localStorage.getItem('editCar'));
      this.editMess=JSON.parse(localStorage.getItem('editCar'));
      this.title=editMess.BRAND+' '+editMess.SET+' '+editMess.MODEL;
      this.carPhone=editMess.LICENCE;//车牌号
      if(editMess.SICON.indexOf('http')>-1){
        this.logo=editMess.SICON;//车logo
      }else{
        this.logo=publicUrl+editMess.SICON;//车logo
      }
      this.mileage=editMess.MILE;//里程数
      this.carCode=editMess.VIN;//车架号
      this.engineCode=editMess.ENGINE_NUM;//发动机号
    }
    if(this.from=='illegal'){
      this.headerTitle='车辆信息';
    }
  },
  // el被创建，并挂载到实例上
  mounted(){

    
  },
  // 方法（实例中的方法）-----------
  methods:{
    //车牌获得焦点
    brandFocus(){
      this.showBrand1=true;
      this.showNum1=false;
      this.deleteFlag=true;
      this.showNum2=false;
      this.showNum3=false;
      this.keyBoards=false;
      this.show4=true;
      this.showFlags=1;
    },
    showBrand(){
      this.showToast=true;
    },
    closeBrand(){
      this.showToast=false;
    },
    //车架号
    codeFocus(){
      this.showBrand2=false;
      this.showNum2=true;
      this.deleteFlag=false;
      this.showNum1=false;
      this.showNum3=false;
      this.keyBoards=false;
      this.show4=false;
      this.showFlags=2;
    },
    //发动机号输入框获得焦点
    engineFocus(){
      this.showBrand3=false;
      this.showNum3=true;
      this.deleteFlag=false;
      this.showNum2=false;
      this.showNum1=false;
      this.keyBoards=false;
      this.show4=false;
      this.showFlags=3;
    },
    //里程数键盘
    mileFocus(){
      this.showBrand1=false;
      this.showNum1=false;
      this.showBrand2=false;
      this.showNum2=false;
      this.showBrand3=false;
      this.showNum3=false;
      this.deleteFlag=false;
      this.keyBoards=true;
      this.show4=false;
      this.showFlags=0;
    },
    //关闭键盘
    broadcast(){
      this.mileFocus();
    },
    //简称键盘输出的值
    change(val){
      this.carPhone=val;
    },
    //26个字母键盘的输出
    brand(val){
      if(this.showNum1){
        this.carPhone=val;
      }else if(this.showNum2){
        this.carCode=val;
      }else if(this.showNum3){
        this.engineCode=val;
      }
    },
    //检测车牌号是否合格
    isLicenseNo(str) {
      var result = false;
      var express = /^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼使领A-Z]{1}[A-Z]{1}[A-Z0-9]{4}[A-Z0-9挂学警港澳]{1}$/;
      if (str.length == 7){
        result = express.test(str);
      }
      return result;
    },
    //更换车辆
    selectCar(){//更换车辆
      this.$router.push('/addCar');
    },
    //违章查询
    illegalCheck(){
      if(!this.isLicenseNo(this.carPhone)){
        this.$vux.alert.show({
          title: '提示',
          content: '车牌号不合法'
        })
        return;
      }
      if(this.carCode==''){
        this.$vux.alert.show({
          title: '提示',
          content: '请输入车架号'
        })
        return;
      }
      if(this.engineCode==''){
        this.$vux.alert.show({
          title: '提示',
          content: '请输入发动机号'
        })
        return;
      }
      let obj={};
      obj.USER_ID=this.userId;
      if(localStorage.getItem('addCar')){
        obj.CARBRAND_ID=this.addCarMess.BRAND_ID;
        obj.CARSET_ID=this.addCarMess.SET_ID;
        obj.CARMODEL_ID=this.addCarMess.MODEL_ID;
        obj.CARBRAND_NAME=this.addCarMess.CARBRAND_NAME;
      }else{
        obj.CAR_ID=this.editMess.CAR_ID;
        obj.CARBRAND_NAME=this.editMess.BRAND+'_'+this.editMess.SET+'_'+this.editMess.MODEL;
      }
      //公共部分的数据
      obj.SCAR_NUMBER=this.carPhone;//车牌号
      obj.FRAME_NO=this.carCode;//车架号
      obj.ENG_NO=this.engineCode;//发动机号
      this.$vux.loading.show({
         text: ''
      });
      this.$fetchPost('/IllegalBaoWeb/realtimeIllegal.action',obj).then(res=>{
          this.$vux.loading.hide();
          localStorage.setItem('illegalSearch',JSON.stringify(res));
          this.$router.push('/illegalHome');
        },err=>{
          console.log(err);
        })
    },
    //添加车辆
    addCar(){
      if(!this.isLicenseNo(this.carPhone)){
        this.$vux.alert.show({
          title: '提示',
          content: '车牌号不合法'
        })
        return;
      }
      if(this.mileage==''){
        this.$vux.alert.show({
          title: '提示',
          content: '请输入里程数'
        })
        return;
      }
      let obj={};
      obj.USER_ID=this.userId;
      obj.TYPE=1;
      let url='/TempCarWeb/saveTempCar.action';//保存车辆地址
      if(localStorage.getItem('editCar')){
        obj.CAR_ID=this.editMess.CAR_ID;//车id
        obj.CARBRAND_ID=this.editMess.BRAND_ID;//车品牌id
        obj.CARSET_ID=this.editMess.SET_ID;//车型id
        obj.CARMODEL_ID=this.editMess.MODEL_ID;//车系id
        obj.CARBRAND_NAME=this.editMess.BRAND+'_'+this.editMess.SET+'_'+this.editMess.MODEL;
        url='/TempCarWeb/updateTempCar.action';//编辑车辆地址
      }else{
        //添加车辆的数据
        obj.CARBRAND_ID=this.addCarMess.BRAND_ID;
        obj.CARSET_ID=this.addCarMess.SET_ID;
        obj.CARMODEL_ID=this.addCarMess.MODEL_ID;
        obj.CARBRAND_NAME=this.addCarMess.CARBRAND_NAME;
      }
      //公共部分的数据
      obj.CAR_NUMBER=this.carPhone;//车牌号
      obj.MILEAGE=this.mileage;//里程数
      obj.CAR_VIN=this.carCode;//车架号
      obj.ENGINE_NUM=this.engineCode;//发动机号
      this.$fetchPost(url,obj).then(res=>{
          if(res.STATUS_CODE==1){
            let that=this;
            if(localStorage.getItem('editCar')){
                this.$router.push('/allCar');
            }else{
                if(localStorage.getItem('selectCars')){//跳转到选择车辆
                    this.$router.push('/selectCar');
                }else if(localStorage.getItem('carFrom')){
                  that.$router.push(localStorage.getItem('carFrom'));
                }else{//跳转到所有车辆
                  that.$router.push('/allCar');
                }
                
            }
          }
        },err=>{
          console.log(err);
        })
    }

  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="less" scoped>
.updateCar{
	width: 7.5rem;
	height: 100%;
  background: #f2f2f2;
	.group{
    border-bottom: 0.2rem solid #eae9e9;
    img{
      width: 0.8rem;
      height: 0.9rem;
    }
  }
  .keyBoards{
    display: none;
  }
  .illegalR{
    .illegalTitle{
      margin: 0.2rem auto;
      img{
        width: 3.76rem;
        height: 0.3rem;
      }
    }
    .illegalFlag{
      padding: 0 0.7rem;
      display: flex;
      justify-content: space-around;
      .flag{
        img{
          height: 0.67rem;
          width: 0.67rem;
          display: block;
          margin: 0 auto;
        }
        p{
          font-size: 0.2rem;
          color:#666;
          padding-top: 0.15rem;
        }
      }
    }
  }
  .updateCarH{
    background: #fff;
    height: 1.4rem;
    display: flex;
    padding: 0 0.2rem;
    margin-bottom:0.2rem;
    img{
      height: 1rem;
      margin-top:0.2rem;
      width: 1rem;
    }
    .carMess{
      text-align: left;
      width: 80%;
      padding-left: 0.2rem;
      h1{
        font-size: 0.3rem;
        font-weight: bolder;
        margin: 0.35rem 0 0.15rem 0;
      }
      p{
        font-size: 0.24rem;
      }
    }
    .right{
      width: 0.36rem;
      height: 0.36rem;
      margin-top:0.5rem;
    }
  }
  .showImg{
    width: 4.9rem;
    height: 3.5rem;
  }
  .item{
    height: 1rem;
    line-height: 1rem;
    padding: 0 0.2rem 0 0.1rem;
    display: flex;
    background: #fff;
    input{
      border: 0;
      height: 1rem;
      line-height: 0.9rem;
      padding-left: 0.15rem;
      font-size: 0.3rem;
      outline: medium;
      width: 100%;
    }
    .inputActive{
      color: #ec2121;
    }
    .inputActive::-webkit-input-placeholder{
      color:#ec2121;
    }
    .inputActive:-moz-placeholder { /* Mozilla Firefox 4 to 18 */  
       color:#A9A9A9;  
       opacity:1;  
    }  
    .inputActive::-moz-placeholder { /* Mozilla Firefox 19+ */  
       color:#A9A9A9;  
       opacity:1;  
    }  
    .inputActive::-ms-input-placeholder { /* Internet Explorer 10+ */  
       color:#A9A9A9;  
    }  
    label{
      font-size: 0.3rem;
      height: 1rem;
      color: #333;
      min-width:1.5rem;
      text-align: center;
      span:first-child{
        padding-right: 0.3rem;
      }
      span:last-child{
        padding-left: 0.3rem;
      }
    }
    img{
      min-width: 0.36rem;
      width:0.36rem;
      height: 0.36rem;
      margin-top:0.3rem;
    }
  }
  .lichen{
    box-sizing: content-box;
    border-bottom:0.2rem solid #eae9e9;
  }
  .save{
    width: 70%;
    margin: 0.4rem auto 0;
    height: 0.75rem;
    font-size: 0.35rem;
    line-height: 0.75rem;
    text-align: center;
    background: #ec2121;
    color: #fff;
    border-radius: 0.15rem;
  }
  .illegalBtn{
    margin: 1.4rem auto 0;
    width: 80%;
  }
  .vux-label{
    font-size: 0.3rem;
  }
}
</style>
